package cn.wolfcode.consumer_test.api._02_send_mode;


import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class Consumer {
    public static void main(String[] args) throws Exception{

        //创建一个消费者
        DefaultMQPushConsumer defaultMQPushConsumer = new DefaultMQPushConsumer("wolfcode-consumer");
        //绑定namesrv地址
        defaultMQPushConsumer.setNamesrvAddr("127.0.0.1:9876");
        //绑定消费主题
        defaultMQPushConsumer.subscribe("_02_send_mode","*");
        //消费者监听并处理消息
        defaultMQPushConsumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> list, ConsumeConcurrentlyContext consumeConcurrentlyContext) {
                for (MessageExt messageExt : list) {
                    System.out.println("消费的信息是" + new String(messageExt.getBody()));
                }
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        //开启消费者并消费消息
        defaultMQPushConsumer.start();
    }
}
